home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11942 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  98 lines

  1. Path: howland.reston.ans.net!agate!parsons
  2. From: parsons@vouvray.CS.Berkeley.EDU (David C. Parsons)
  3. Newsgroups: comp.lang.c++
  4. Subject: Need help on static data members of class templates
  5. Date: 17 Mar 1996 05:09:03 GMT
  6. Organization: University of California, Berkeley
  7. Message-ID: <4ig6pf$aqp@agate.berkeley.edu>
  8. NNTP-Posting-Host: vouvray.cs.berkeley.edu
  9.  
  10. I've been having a lot of difficulty getting static data members of
  11. class templates to work right.  Specifically, things get very weird
  12. when I try to instantiate the template with the type parameter equal
  13. to another instantiation of the template.  The following is boiled
  14. down from the code that gives me the problem:
  15.  
  16. //-- begin code sample --------------------------------
  17.  
  18. #include <iostream.h>
  19.  
  20. template< class T >
  21. class C
  22. {
  23. public:
  24.  
  25.     static const T classVar;
  26.     C()
  27.     {
  28.     cout << "instantiating C: classVar is " << classVar << endl;
  29.     i = 100;  // this is line 12
  30.     }
  31.     int i;
  32. };  // end of class C
  33.  
  34. template< class T >
  35. ostream& operator <<( ostream& os, const C< T >& c )
  36. {
  37.     os << "[object of class C having classVar " << c.classVar << "]";
  38.     return os;
  39. }
  40.  
  41. // initialize static members of template instantiations we'll need
  42.  
  43. const int C<int>::classVar = 3;
  44. const C< int > C< C<int> >::classVar = C<int>();  // this is line 27
  45.  
  46. int main()
  47. {
  48.     C< int > c1;
  49.     C< C<int> > c2;
  50. }
  51.  
  52. //-- end code sample --------------------------------
  53.  
  54. This compiles fine under g++ (with only a warning about unused
  55. variables c1 and c2), but when run it produces:
  56.  
  57. instantiating C: classVar is 3
  58. Bus error (core dumped)
  59.  
  60. From gdb, it appears that the error is occurring during the
  61. construction of the C< C<int> >::classVar object:
  62.  
  63. #0  0x4590 in C<int>::C (this=0x36b0) at scratch.C:12
  64. #1  0x4478 in global constructors keyed to
  65.            C<int>::classVar () at scratch.C:27
  66. #2  0x1d00c in __do_global_ctors ()
  67. #3  0x1d050 in __main ()
  68. #4  0x43a0 in main () at scratch.C:30
  69.  
  70. Surprisingly (to me at least), the whole problem goes away if I
  71. comment out the assignment i = 100; in the constructor (line 12
  72. above).  The output is then:
  73.  
  74. instantiating C: classVar is 3
  75. instantiating C: classVar is 3
  76. instantiating C: classVar is [object of class C having classVar 3]
  77.  
  78. In summary, it seems that assigning to any data member in the
  79. constructor makes it impossible to instantiate the template with
  80. another instance of the same template.  I do not understand this
  81. restriction. 
  82.  
  83. I've tried every variation of this code I can think of, but nothing
  84. seems to work.  Anyone have any ideas?  I'd really like to get this to
  85. work, as it forms part of an otherwise very elegant sparse matrix
  86. package.
  87.  
  88. David Parsons
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.